<?php

namespace App\Http\Controllers\Company\English;
use Illuminate\Http\Request;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyUser;
use App\Http\Requests\Company\Register\CompanyBasicRequest;
use Style;


class RegisterController extends CommonController{
   public function index(){
       return Style::view('company.auth.register');
   }

    /**
     * 分帧地图
     * @return mixed
     */
    public function iframeMap(){
        return Style::view('company.english.iframeMap');
    }

    /**
     * ajax验证用户名
     * @param Request $request
     */
    public function ajaxUsername(Request $request){
        $username = $request->input('username');
        $result = CompanyUser::where('username', $username)->first();
        //判断用户名是不是被注册
        echo ($result) ? 'false' : 'true';
    }

    /**
     * 企业基本信息注册
     * @return mixed
     */
    public function registerBasic(){
        $userInfo = auth()->guard('company')->user();
        $phone = ($userInfo->phone) ? explode('-',$userInfo->phone) : [];
        return Style::view('company.registerBasic',compact('userInfo','phone'));
    }

    /**
     * 企业注册post
     * @param CompanyBasicRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postRegisterBasic(CompanyBasicRequest $request){
        $input = $request->only('company_name', 'brands', 'comkind', 'industry', 'established', 'employee_num', 'temptation','profile',
            'contacts','mobile','phone','fax','email','qq','region','address','longitude','latitude','coordinate_address','website','lines');
        $phone =  trim($request->input('area_code')) . '-' . trim($request->input('phone'));
        $phone .= trim($request->input('ext'))?"-".trim($request->input('ext')):'';
        $phone = trim($phone,'-');

        //获取用户信息ID
        $userInfo = auth()->guard('company')->user();
        $input['uid'] = $userInfo->uid;
        $input['phone'] = $phone;
        $input['addtime'] = date('Y-m-d H:i:s',time());
        $input['updatetime'] = date('Y-m-d H:i:s',time());

        $companyBasic = CompanyBasic::where('uid',$userInfo->uid)->count();
        if($companyBasic){
            return Style::view('errors.404', ['exception' => '企业基本信息已经注册...'], 404);
        }else{
            CompanyBasic::create($input);
            return redirect('company/welcome');
        }
    }
}
